package com.lovo.upload.controller;

import com.lovo.upload.util.FileUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
public class FileController {
    @PostMapping("upload")
    public String upload(@RequestParam("fileName")MultipartFile file) throws IOException {
         //验证格式
        //重命名
        String operatingSystem = System.getProperty("os.name");
        boolean isWindows = operatingSystem.startsWith("Windows");
        String path="";
        if(isWindows){
            path="imgW/"+file.getOriginalFilename();
          file.transferTo(new File(FileUtil.FILE_PATH_W+file.getOriginalFilename()));
        } else {
            path="img/"+file.getOriginalFilename();
            file.transferTo(new File(FileUtil.FILE_PATH_L+file.getOriginalFilename()));
        }
        return path;
    }



}
